package com.travel.utils;

import java.text.SimpleDateFormat;
import java.util.Date;

public class DateAddDay {
	 public static Date addAndSubtractDaysByGetTime(Date dateTime/*待处理的日期*/,int n/*加减天数*/){ 

	     //日期格式 
	     SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");  
	     SimpleDateFormat dd=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");     

	     System.out.println(df.format(new Date(dateTime.getTime() + n * 24 * 60 * 60 * 1000L))); 
	     //System.out.println(dd.format(new Date(dateTime.getTime() + n * 24 * 60 * 60 * 1000L))); 
	     //注意这里一定要转换成Long类型，要不n超过25时会出现范围溢出，从而得不到想要的日期值 
	     return new Date(dateTime.getTime() + n * 24 * 60 * 60 * 1000L); 
	   } 

	   public static Date addAndSubtractDaysByCalendar(Date dateTime/*待处理的日期*/,int n/*加减天数*/){ 

	     //日期格式 
	     SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");  
	     SimpleDateFormat dd=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  

	     java.util.Calendar calstart = java.util.Calendar.getInstance(); 
	       calstart.setTime(dateTime); 

	     calstart.add(java.util.Calendar.DAY_OF_WEEK, n);  

	     System.out.println(df.format(calstart.getTime())); 
	     //System.out.println(dd.format(calstart.getTime())); 
	     return calstart.getTime(); 
	   } 

}
